package liuh.com.ledlamp.popup;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.blankj.utilcode.utils.ToastUtils;

import liuh.com.ledlamp.R;
import liuh.com.ledlamp.adapter.NormalRecyclerViewAdapter;

/**
 * @author :liuh
 * @note
 * @date: 2017-01-06
 */

public class InputDialog {
    Context c;
    int position;
    NormalRecyclerViewAdapter mRecyclerViewAdapter;
    //
    AlertDialog alertDialogAndroid;

    public InputDialog(Context context, NormalRecyclerViewAdapter mRecyclerViewAdapter,
                       int position) {
        //
        this.c = context;
        this.mRecyclerViewAdapter = mRecyclerViewAdapter;
        this.position = position;
        initView();
    }

    private void initView() {
        LayoutInflater layoutInflaterAndroid = LayoutInflater.from(c);
        View mView = layoutInflaterAndroid.inflate(R.layout.user_input_dialog_box, null);
        AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(c);
        //alertDialogBuilderUserInput.setTitle("内容");
        alertDialogBuilderUserInput.setView(mView);
        final EditText mInputEdittext = (EditText) mView.findViewById(R.id.userInputDialog);
        if (position != -1) {
            //InputEdittext 显示要修改内容
            mInputEdittext.setText(mRecyclerViewAdapter.getTitles().get(position));
        }
        alertDialogBuilderUserInput
                //.setCancelable(false)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogBox, int id) {
                        // ToDo get user input here
                        boolean result = true;
                        if (position != -1) {
                            // 修改内容
                            result = mRecyclerViewAdapter.updTitle(position, mInputEdittext.getText().toString());
                        } else {
                            // 增加内容
                            result = mRecyclerViewAdapter.addTitle(mInputEdittext.getText().toString());
                        }
                        dialogBox.cancel();
                        if (!result) {
                            ToastUtils.showLongToast(c, "操作失败，不能设置重复内容!");
                        }
                    }
                })

                .setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialogBox, int id) {
                                dialogBox.cancel();
                            }
                        });

        alertDialogAndroid = alertDialogBuilderUserInput.create();
    }

    public void showInputDialog() {
        alertDialogAndroid.show();
    }

}
